package models

import (
	"cart_srv/appconfig"
	"fmt"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"log"
)

var DB *gorm.DB

func Init() {
	var err error
	data := appconfig.ConfigData.Mysql
	dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local", data.User, data.Password, data.Host, data.Port, data.DataBase)
	DB, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})
	if err != nil {
		panic("数据库连接失败")
	} else {
		log.Print("数据库连接成功")
	}
	err = DB.AutoMigrate(&User{}, &Goods{})
	if err != nil {
		panic("迁移失败")
	} else {
		log.Print("数据表迁移成功")
	}
}
